package cn.edu.scau.cmi.ema.dao;

import cn.edu.scau.cmi.ema.base.CmiPagination;
import cn.edu.scau.cmi.ema.domain.Purchaseadditive;
import cn.edu.scau.cmi.ema.util.CmiSqlUtil;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.math.BigDecimal;
import java.util.*;

@Repository("PurchaseadditiveDAO")
@Transactional
public class PurchaseadditiveDAOImpl extends AbstractJpaDao<Purchaseadditive> implements PurchaseadditiveDAO {

	private final static Set<Class<?>> dataTypes = new HashSet<Class<?>>(
			Arrays.asList(new Class<?>[] { Purchaseadditive.class }));

	@PersistenceContext()
	private EntityManager entityManager;

	public PurchaseadditiveDAOImpl() {
		super();
	}

	public EntityManager getEntityManager() {
		return entityManager;
	}

	public Set<Class<?>> getTypes() {
		return dataTypes;
	}

	@Transactional
	public Purchaseadditive findPurchaseadditiveById(Integer id) throws DataAccessException {

		return findPurchaseadditiveById(id, -1, -1);
	}

	@Transactional
	public Purchaseadditive findPurchaseadditiveById(Integer id, int startResult, int maxRows)
			throws DataAccessException {
		try {
			Query query = createNamedQuery("findPurchaseadditiveById", startResult, maxRows, id);
			return (Purchaseadditive) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Purchaseadditive> findPurchaseadditiveByName(String name) throws DataAccessException {

		return findPurchaseadditiveByName(name, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Purchaseadditive> findPurchaseadditiveByName(String name, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findPurchaseadditiveByName", startResult, maxRows, name);
		return new LinkedHashSet<Purchaseadditive>(query.getResultList());
	}

	@Transactional
	public Set<Purchaseadditive> findPurchaseadditiveByNameContaining(String name) throws DataAccessException {

		return findPurchaseadditiveByNameContaining(name, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Purchaseadditive> findPurchaseadditiveByNameContaining(String name, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findPurchaseadditiveByNameContaining", startResult, maxRows, name);
		return new LinkedHashSet<Purchaseadditive>(query.getResultList());
	}

	@Transactional
	public Set<Purchaseadditive> findPurchaseadditiveByDate(Calendar date) throws DataAccessException {

		return findPurchaseadditiveByDate(date, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Purchaseadditive> findPurchaseadditiveByDate(Calendar date, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findPurchaseadditiveByDate", startResult, maxRows, date);
		return new LinkedHashSet<Purchaseadditive>(query.getResultList());
	}

	@Transactional
	public Set<Purchaseadditive> findPurchaseadditiveByPrice(BigDecimal price) throws DataAccessException {

		return findPurchaseadditiveByPrice(price, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Purchaseadditive> findPurchaseadditiveByPrice(BigDecimal price, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findPurchaseadditiveByPrice", startResult, maxRows, price);
		return new LinkedHashSet<Purchaseadditive>(query.getResultList());
	}

	@Transactional
	public Set<Purchaseadditive> findPurchaseadditiveByQuantity(BigDecimal quantity) throws DataAccessException {

		return findPurchaseadditiveByQuantity(quantity, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Purchaseadditive> findPurchaseadditiveByQuantity(BigDecimal quantity, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findPurchaseadditiveByQuantity", startResult, maxRows, quantity);
		return new LinkedHashSet<Purchaseadditive>(query.getResultList());
	}

	@Transactional
	public Set<Purchaseadditive> findPurchaseadditiveByDescription(String description) throws DataAccessException {

		return findPurchaseadditiveByDescription(description, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Purchaseadditive> findPurchaseadditiveByDescription(String description, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findPurchaseadditiveByDescription", startResult, maxRows, description);
		return new LinkedHashSet<Purchaseadditive>(query.getResultList());
	}

	@Transactional
	public Set<Purchaseadditive> findPurchaseadditiveByDescriptionContaining(String description)
			throws DataAccessException {

		return findPurchaseadditiveByDescriptionContaining(description, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Purchaseadditive> findPurchaseadditiveByDescriptionContaining(String description, int startResult,
			int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findPurchaseadditiveByDescriptionContaining", startResult, maxRows,
				description);
		return new LinkedHashSet<Purchaseadditive>(query.getResultList());
	}

	@Transactional
	public Purchaseadditive findPurchaseadditiveByPrimaryKey(Integer id) throws DataAccessException {
		return findPurchaseadditiveByPrimaryKey(id, -1, -1);
	}

	@Transactional
	public Purchaseadditive findPurchaseadditiveByPrimaryKey(Integer id, int startResult, int maxRows)
			throws DataAccessException {
		try {
			Query query = createNamedQuery("findPurchaseadditiveByPrimaryKey", startResult, maxRows, id);
			return (Purchaseadditive) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Purchaseadditive> findPurchaseadditivesByIds(Set<Integer> ids) throws DataAccessException {
		try {
			Query query = createNamedQuery("findPurchaseadditivesByIds", -1, -1, ids);
			return new HashSet(query.getResultList());
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Purchaseadditive> findAllPurchaseadditives() throws DataAccessException {

		return findAllPurchaseadditives(-1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Purchaseadditive> findAllPurchaseadditives(int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findAllPurchaseadditives", startResult, maxRows);
		return new LinkedHashSet<Purchaseadditive>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	public Set<Purchaseadditive> findPurchaseadditivesByAdditive(Integer additiveId, int start, int max)
			throws DataAccessException {
		Query query = createNamedQuery("findAllPurchaseadditivesByAdditive", start, max, additiveId);
		return new LinkedHashSet<Purchaseadditive>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	public Set<Purchaseadditive> findPurchaseadditivesByProducer(Integer producerId, int start, int max)
			throws DataAccessException {
		Query query = createNamedQuery("findAllPurchaseadditivesByProducer", start, max, producerId);
		return new LinkedHashSet<Purchaseadditive>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	public Set<Purchaseadditive> findPurchaseadditivesBySupplier(Integer supplierId, int start, int max)
			throws DataAccessException {
		Query query = createNamedQuery("findAllPurchaseadditivesBySupplier", start, max, supplierId);
		return new LinkedHashSet<Purchaseadditive>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	public Set<Purchaseadditive> findPurchaseadditivesByUnit(Integer unitId, int start, int max)
			throws DataAccessException {
		Query query = createNamedQuery("findAllPurchaseadditivesByUnit", start, max, unitId);
		return new LinkedHashSet<Purchaseadditive>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Purchaseadditive> findPurchaseadditiveByCompositeProperities(String sql, int startResult, int maxRows) {
		return new LinkedHashSet<Purchaseadditive>(createQuery(sql, startResult, maxRows).getResultList());
	}

	@Transactional
	public Set<Purchaseadditive> findPurchaseadditivesByDetachedCriteria(CmiPagination page)
			throws DataAccessException {
		DetachedCriteria detachedCriteria = CmiSqlUtil.createDetachedCriteria(page, Purchaseadditive.class);
		Session session = (Session) entityManager.getDelegate();
		Set<Purchaseadditive> purchaseadditives = new HashSet(detachedCriteria.getExecutableCriteria(session)
				.setFirstResult(page.getSearchBeginCursor()).setMaxResults(page.getPaginationSize()).list());

		return purchaseadditives;
	}

	@Transactional
	public Purchaseadditive findPurchaseadditiveByPhysicalPrimaryKey(Integer producerId, Integer supplierId,
			Integer additiveId, Calendar date) throws DataAccessException {
		return findPurchaseadditiveByPhysicalPrimaryKey(producerId, supplierId, additiveId, date, -1, -1);
	}

	@Transactional
	public Purchaseadditive findPurchaseadditiveByPhysicalPrimaryKey(Integer producerId, Integer supplierId,
			Integer additiveId, Calendar date, int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findPurchaseadditiveByPhyscialPrimaryKey", startResult, maxRows, producerId,
					supplierId, additiveId, date);
			return (Purchaseadditive) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	public boolean canBeMerged(Purchaseadditive entity) {
		return true;
	}
}
